<?php
declare (strict_types=1);

namespace app\common\listener;

use app\common\model\Relation;
use app\common\model\User;
use app\common\model\UserLevelFlag;
use app\common\model\UserLevelLog;
use think\facade\Event;
use think\facade\Log;

class UserLevel
{
    /**
     * 会员等级升级VIP付款后升级，店主确认收货后7天升级
     * @param $order
     */
    public function handle($order)
    {
        try {
            $user = User::find($order->uid);
            $puser = User::find($user->parent_id);
            $this->level($order, $user, $puser);
        } catch (\Exception $e) {
            Log::write("listener.UserLevel:" . $e->getMessage());
            return $e->getMessage();
        }
    }

    /**
     * 会员级别
     * @param $order
     * @param $user
     * @param $puser
     * @return bool
     */
    private function level($order, $user, $puser)
    {
        $level = \app\common\model\UserLevel::whereIn('id', [4, 5])->column('shopers', 'id');
        $high_min = $level[4];
        $leader_min = $level[5];
        if ($order->order_type == 1) {//VIP区
            if ($user->level_id < 2) {
                $before_level_id = $user->level_id;
                $user->level_id = 2;//升级为VIP会员
                $user->syn_level = 2;
                $user->vip_time = time();
                //更改激活状态
                if($user->is_active != 2){
                    $user->is_active = 2;
                    $user->active_time = time();
                }
                $user->save();
                $this->log($user, $before_level_id,$user->level_id);
            }
            //更改激活状态
            if($user->is_active != 2){
                $user->is_active = 2;
                $user->active_time = time();
            }
        } elseif ($order->order_type == 2) {//店主区
            if ($user->level_id < 3) {
                $self_shopers = User::where(['parent_id' => $user->id, 'is_del' => 0])->where('syn_level', '>=', 3)->count();
                $before_level_id = $user->level_id;
                if ($self_shopers >= $high_min) {//9配置：邀请店主及店主以上级别人数
                    $user->level_id = 4;//升级为高级店主
                    if($user->syn_level < 4){
                        $user->syn_level = 4;
                    }
                } else {
                    $user->level_id = 3;//升级为店主
                    if($user->syn_level < 3){
                        $user->syn_level = 3;
                    }
                    $user->shoper_time = time();
                    $user->exp_shoper_time = strtotime("+1 year");
                }
                //更改激活状态
                if($user->is_active != 2){
                    $user->is_active = 2;
                    $user->active_time = time();
                }
                $user->save();
                if(!isset($order->has_log)){
                    $this->log($user, $before_level_id,$user->level_id);
                }

                if ($puser->syn_level == 3) {
                    $shopers = User::where(['parent_id' => $user->parent_id, 'is_del' => 0])->where('syn_level', '>=', 3)->count();
                    if ($shopers >= $high_min) {//9配置：邀请店主及店主以上级别人数
                        $before_level_id = $puser->level_id;
                        $puser->level_id = 4;
                        $t = 31536000;//一年
                        $puser->expire_time = $puser->expire_time ? $puser->getData('expire_time') + $t : strtotime(User::getExpTime($puser)) + $t;
                        if($puser->syn_level < 4){
                            $puser->syn_level = 4;
                        }

                        $puser->save();
                        $this->log($puser, $before_level_id, $puser->syn_level);
                    }
                }
                $this->becomeLeader($user, $leader_min);
            }
        }

        return true;
    }

    private function becomeLeader($user, $leader_min, $level = 1)
    {
        if($level <= 3){
            if($puser = User::find($user->parent_id)){
                $this->leader($puser, $leader_min);
                $level ++;
                $this->becomeLeader($puser, $leader_min, $level);
            }
        }
        return false;
    }

    private function leader($puser, $leader_min)
    {
        $relations = Relation::three($puser);//三代内
        $shopers = User::whereIn('id', $relations)->where('syn_level', '>=', 3)->count();
        if ($shopers >= $leader_min) {//225配置：邀请店主及店主以上级别人数
            $before_level_id = $puser->level_id;
            $puser->level_id = 5;
            $puser->leader_time = time();
            $puser->exp_leader_time = strtotime("+1 year");
            $t = 31536000;//一年
            $puser->expire_time = $puser->expire_time ? $puser->getData('expire_time') + $t : strtotime(User::getExpTime($puser)) + $t;
            if($puser->syn_level < 5) {
                $puser->syn_level = 5;
            }
            $puser->save();
            $this->log($puser, $before_level_id, $puser->level_id);

            (new UserLevelFlag())->flag($puser);
        }

        return false;
    }

    /**
     * 会员级别升级日志
     * @param $user
     * @param $before_level_id
     * @param $after_level_id
     * @return bool
     */
    private function log($user, $before_level_id, $after_level_id): bool
    {
        return UserLevelLog::levelLog($user, 1, $before_level_id, $after_level_id);
    }

}